package com.monkey.tensquare.spit.controller;

import com.monkey.tensquare.spit.entity.Spit;
import com.monkey.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spit")
public class SpitController {
    @Autowired
    private SpitService spitService;
    @PostMapping
    public Result add(@RequestBody Spit spit){
        try {
            spitService.add(spit);
            return new Result(StateCode.OK,true,"吐槽成功！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"吐槽失败！",null);
        }
    }
    @GetMapping
    public Result findAll(){
        try {
            List<Spit> spitList = spitService.findAll();
            return new Result(StateCode.OK,true,"查询吐槽列表成功！",spitList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"查询吐槽列表失败！",null);
        }
    }
    @GetMapping("{spitId}")
    public Result findById(@PathVariable String spitId){
        try {
            Spit spit = spitService.findById(spitId);
            return new Result(StateCode.OK,true,"查询吐槽成功！",spit);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"查询吐槽失败！",null);
        }
    }
    @PutMapping("{spitId}")
    public Result update(@PathVariable String spitId, @RequestBody Spit spit){
        try {
            spitService.update(spitId,spit);
            return new Result(StateCode.OK,true,"修改吐槽成功！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"修改吐槽失败！",null);
        }
    }
    @DeleteMapping("{spitId}")
    public Result delete(@PathVariable String spitId){
        try {
            spitService.delete(spitId);
            return new Result(StateCode.OK,true,"删除吐槽成功！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"删除吐槽失败！",null);
        }
    }
    @PutMapping("/thumbup/{spitId}")
    public Result thumbup(@PathVariable String spitId){
        try {
            boolean b = spitService.thumbup(spitId);
            if (b){
                return new Result(StateCode.OK,true,"点赞成功！",null);
            }else {
                return new Result(StateCode.ERROR,false,"不能重复点赞！",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"点赞失败！",null);
        }
    }
    @GetMapping("/comment/{parentid}/{page}/{size}")
    public Result comment(@PathVariable String parentid,@PathVariable Integer page,@PathVariable Integer size){
        try {
            PageResult<Spit> spitPageResult = spitService.comment(parentid,page,size);
            return new Result(StateCode.OK,true,"查询成功！",spitPageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"查询失败！",null);
        }
    }
    @PostMapping("/search/{page}/{size}")
    public Result search(@RequestBody Spit spit, @PathVariable Integer page,@PathVariable Integer size){
        try {
            PageResult<Spit> spitPageResult = spitService.search(spit,page,size);
            return new Result(StateCode.OK,true,"查询成功！",spitPageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"查询失败！",null);
        }
    }
}
